const AElfModule = require('../models/aelf')
const PkManageModule = require('../models/private_key_manage')
const HistoryModule = require('../models/history')

exports.form = (req, res) => {
    res.render('check_state', { title: '检查版权状态' });
};

exports.check_state = (req, res) => {
    const data = req.query;
    console.log(data);
    const aelf = new AElfModule();

    if (data.CRT_id == '' || data.Visitor == '') {
        res.send({ code: 'error' })
    } else {
        PkManageModule.get_priKey_by_user(data.Visitor, (user) => {
            aelf.check_chain_state((state) => {
                if (state) {
                    aelf.load_wallet(user.priKey, (wallet) => {
                        if(typeof(wallet) !== 'undefined'){
                            aelf.load_contract(true , (param) =>{
                                console.log("contract prepared");
                                aelf.check_cr_state(param, data.CRT_id,(ret)=>{
                                    res.send(ret);
                                });
                            });
                        }
                    });
                }
            })
        });
    }
}